<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="图片ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入图片ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="源文章ID" prop="sourceId">
        <el-input
          v-model="queryParams.sourceId"
          placeholder="请输入源文章ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章来源" prop="sourceUrl">
        <el-input
          v-model="queryParams.sourceUrl"
          placeholder="请输入文章来源"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="哈希值" prop="hashName">
        <el-input
          v-model="queryParams.hashName"
          placeholder="请输入哈希值"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['news:image:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['news:image:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['news:image:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['news:image:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="imageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="图片ID" align="center" prop="id" />
      <el-table-column label="源文章ID" align="center" prop="sourceId" />
      <!-- <el-table-column label="文章来源" align="center" prop="sourceUrl" /> -->
      <el-table-column label="media_id(未知1)" align="center" prop="mediaId" />
      <el-table-column label="哈希值" align="center" prop="hashName" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="爬虫服务器上的地址" align="center" prop="physicalAddress">
        <template #default="scope">
          <image-preview :src="scope.row.physicalAddress" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="ftp服务器上的地址" align="center" prop="compressAddress">
        <template #default="scope">
          <image-preview :src="scope.row.compressAddress" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="modifyTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.modifyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序ID" align="center" prop="sortId" />
      <el-table-column label="高度" align="center" prop="height" />
      <el-table-column label="宽度" align="center" prop="width" />
      <el-table-column label="news_type(未知1)" align="center" prop="newsType" />
      <el-table-column label="图片url" align="center" prop="imgUrl">
        <template #default="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="use_type(未知0)" align="center" prop="useType" />
      <el-table-column label="status(未知0)" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['news:image:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['news:image:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图片对话框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body>
      <el-form ref="imageRef" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="源文章ID" prop="sourceId">
          <el-input v-model="form.sourceId" placeholder="请输入源文章ID" />
        </el-form-item>
        <el-form-item label="文章来源" prop="sourceUrl">
          <el-input v-model="form.sourceUrl" placeholder="请输入文章来源" />
        </el-form-item>
        <el-form-item label="media_id(未知1)" prop="mediaId">
          <el-input v-model="form.mediaId" placeholder="请输入media_id(未知1)" />
        </el-form-item>
        <el-form-item label="哈希值" prop="hashName">
          <el-input v-model="form.hashName" placeholder="请输入哈希值" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="爬虫服务器上的地址" prop="physicalAddress">
          <el-input v-model="form.physicalAddress" placeholder="请输入爬虫服务器上的地址" />
        </el-form-item>
        <el-form-item label="ftp服务器上的地址" prop="compressAddress">
          <el-input v-model="form.compressAddress" placeholder="请输入ftp服务器上的地址" />
        </el-form-item>
        <el-form-item label="排序ID" prop="sortId">
          <el-input v-model="form.sortId" placeholder="请输入排序ID" />
        </el-form-item>
        <el-form-item label="高度" prop="height">
          <el-input v-model="form.height" placeholder="请输入高度" />
        </el-form-item>
        <el-form-item label="宽度" prop="width">
          <el-input v-model="form.width" placeholder="请输入宽度" />
        </el-form-item>
        <el-form-item label="图片url" prop="imgUrl">
          <el-input v-model="form.imgUrl" placeholder="请输入图片url" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Image">
import { listImage, getImage, delImage, addImage, updateImage } from "@/api/news/image";

const { proxy } = getCurrentInstance();

const imageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    sourceId: null,
    sourceUrl: null,
    hashName: null,
  },
  rules: {
    sourceId: [
      { required: true, message: "源文章ID不能为空", trigger: "blur" }
    ],
    sourceUrl: [
      { required: true, message: "文章来源不能为空", trigger: "blur" }
    ],
    mediaId: [
      { required: true, message: "media_id(未知1)不能为空", trigger: "blur" }
    ],
    hashName: [
      { required: true, message: "哈希值不能为空", trigger: "blur" }
    ],
    description: [
      { required: true, message: "描述不能为空", trigger: "blur" }
    ],
    physicalAddress: [
      { required: true, message: "爬虫服务器上的地址不能为空", trigger: "blur" }
    ],
    compressAddress: [
      { required: true, message: "ftp服务器上的地址不能为空", trigger: "blur" }
    ],
    modifyTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ],
    sortId: [
      { required: true, message: "排序ID不能为空", trigger: "blur" }
    ],
    height: [
      { required: true, message: "高度不能为空", trigger: "blur" }
    ],
    width: [
      { required: true, message: "宽度不能为空", trigger: "blur" }
    ],
    newsType: [
      { required: true, message: "news_type(未知1)不能为空", trigger: "change" }
    ],
    imgUrl: [
      { required: true, message: "图片url不能为空", trigger: "blur" }
    ],
    useType: [
      { required: true, message: "use_type(未知0)不能为空", trigger: "change" }
    ],
    status: [
      { required: true, message: "status(未知0)不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询图片列表 */
function getList() {
  loading.value = true;
  listImage(queryParams.value).then(response => {
    imageList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    sourceId: null,
    sourceUrl: null,
    mediaId: null,
    hashName: null,
    description: null,
    physicalAddress: null,
    compressAddress: null,
    modifyTime: null,
    sortId: null,
    height: null,
    width: null,
    newsType: null,
    imgUrl: null,
    useType: null,
    status: null
  };
  proxy.resetForm("imageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加图片";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getImage(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改图片";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["imageRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateImage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addImage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除图片编号为"' + _ids + '"的数据项？').then(function() {
    return delImage(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('news/image/export', {
    ...queryParams.value
  }, `image_${new Date().getTime()}.xlsx`)
}

getList();
</script>
